package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class InboxService {

	@Autowired
	private InboxDao inboxDao;

	private QInbox $ = QInbox.inbox;

	@Autowired
	public void setInboxDao(InboxDao inboxDao) {
		this.inboxDao = inboxDao;
	}

	public List<Inbox> findAll() {
		return ListUtil.toList(inboxDao.findAll());
	}

	public List<Inbox> findAll(Long personId, Long senderId,
			Date receiveTimeBegin, Date receiveTimeEnd, String messageContent,
			String status, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		if (IdUtil.isNotEmpty(senderId)) {
			condition.and($.sender.id.eq(senderId));
		}

		if (receiveTimeBegin != null) {
			condition.and($.receiveTime.after(receiveTimeBegin));
		}

		if (receiveTimeEnd != null) {
			condition.and($.receiveTime.before(receiveTimeEnd));
		}

		if (StringUtil.isNotEmpty(messageContent)) {
			condition.and($.messageContent.contains(messageContent));
		}

		if (StringUtil.isNotEmpty(status)) {
			condition.and($.status.contains(status));
		}

		return ListUtil.toList(inboxDao.findAll(condition));
	}

	public Page<Inbox> findAll(Long personId, Long senderId,
			Date receiveTimeBegin, Date receiveTimeEnd, String messageContent,
			String status, int pageNumber, int pageSize, String sortAttribute,
			String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}
		if (IdUtil.isNotEmpty(senderId)) {
			condition.and($.sender.id.eq(senderId));
		}
		if (receiveTimeBegin != null) {
			condition.and($.receiveTime.after(receiveTimeBegin));
		}

		if (receiveTimeEnd != null) {
			condition.and($.receiveTime.before(receiveTimeEnd));
		}
		if (StringUtil.isNotEmpty(messageContent)) {
			condition.and($.messageContent.contains(messageContent));
		}
		if (StringUtil.isNotEmpty(status)) {
			condition.and($.status.contains(status));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return inboxDao.findAll(condition, pageRequest);
	}
	
	public Long findCount(Long personId, Long senderId,Date receiveTimeBegin, 
			Date receiveTimeEnd, String messageContent, String status) {
		BooleanBuilder condition = new BooleanBuilder();
		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}
		if (IdUtil.isNotEmpty(senderId)) {
			condition.and($.sender.id.eq(senderId));
		}
		if (receiveTimeBegin != null) {
			condition.and($.receiveTime.after(receiveTimeBegin));
		}
		
		if (receiveTimeEnd != null) {
			condition.and($.receiveTime.before(receiveTimeEnd));
		}
		if (StringUtil.isNotEmpty(messageContent)) {
			condition.and($.messageContent.contains(messageContent));
		}
		if (StringUtil.isNotEmpty(status)) {
			condition.and($.status.eq(status));
		}
		return inboxDao.count(condition);
	}

	public Inbox getInbox(Long id) {
		return inboxDao.getOne(id);
	}

	public void deleteInbox(Long id) {
		inboxDao.delete(id);
	}

	public void saveInbox(Inbox inbox) {
		if(null == inbox.getReceiveTime()){
			inbox.setReceiveTime(DateUtil.nowDate());
		}
		inboxDao.save(inbox);
	}

	public void readedInbox(Inbox inbox) {
		Inbox sourceInbox = this.inboxDao.findOne(inbox.getId());
		sourceInbox.setStatus(inbox.getStatus());
		
		this.inboxDao.save(sourceInbox);
	}
	public Inbox findOne(Long id) {
		return this.inboxDao.findOne(id);
	}

}
